package com.woniuxy.shiro;
/*
* 过滤器 BasicHttpAuthenticationFilter 内部底层有一堆过滤器，来处理请求的消息
*
* 重点shiro的配置
*  v    1.isAccessAllowed 是否允许访问
*       2.isLoginAttempt 校验请求头中有咩有携带token
*       3.executeLogin
* */

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JWTFilter extends BasicHttpAuthenticationFilter {
    /*
     * Spring的拦截器：preHandle->controller->postHandler->view->afterHandler
     * 这个preHandle通常用来处理跨域，全局跨域处理
     * 重要的过滤器配置，需要我们自己开发，获取请求头重中的信息，然后执行subject.login(new MyJsonWebToken())
     *
     * */
   /* @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        HttpServletResponse httpServletResponse=(HttpServletResponse)response;
        httpServletResponse.setHeader("Access-control-Allow-Origin",httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers",httpServletRequest.getHeader("Access-Control-Request-Headers"));

        //跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
        *//*if(httpServletRequest.getMethod(),equals(RequestMethod.OPTIONS.name())){
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }*//*
            return super.preHandle(request,response);
    }*/
    /*
    *
    * 是否允许访问到后端，也就是看你请求头有没有带上token*/

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        //判断请求头有没有信息
        if(isLoginAttempt(request,response)){
            //执行登录操作
            try {
                executeLogin(request,response);
            } catch (Exception e) {
                response401(response);
                return false;
            }
        }
         else{
             //如果请求头中没有token，我们应该跳转后台的controller返回一个信息给前端，告诉他还没还有登录 401 没有权限
             return false;
        }
        return true;
    }

    /*
    * 重写shiro底层，过滤流程，这个方法主要时判断请求头中有咩有token信息
    * */

    @Override
    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
        HttpServletRequest request1 = (HttpServletRequest) request;
        //从请求头中获取token信息，Authorization 就是自定义的key
        String token = request1.getHeader("Authorization");
        return token==null?false:true;
    }

    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) {
        HttpServletRequest request1 = (HttpServletRequest) request;
        //请求头中获取token信息，Authorization就是自定义的key
        String token = request1.getHeader("Authorization");
        Subject subject = getSubject(request, response);
        MyJsonWebToken myJsonWebToken = new MyJsonWebToken(token);
        subject.login(myJsonWebToken);
        return true;
    }
    public void response401(ServletResponse response){
        try {
            HttpServletResponse response1=(HttpServletResponse) response;
            response1.sendRedirect("/401");
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
}
